﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AgencyEntityLib.BaseEntity;
using Ecopsy.DataTier;

/// <summary>
/// Summary description for ws_SheduleFace
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class ws_SheduleFace : System.Web.Services.WebService
{

    public ws_SheduleFace()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        AgencyEntityLib.Context.AgencyLib_GlobalConstants._getAgencyId = GlobalConstants.GetAgencyId;
        AgencyEntityLib.Context.AgencyLib_GlobalConstants._getAgency = GlobalConstants.GetAgency;
        Agency a = AgencyEntityLib.Context.AgencyLib_GlobalConstants._getAgency();
    }

    

    /// <summary>
    /// 
    /// </summary>
    /// <param name="SideID"></param>
    /// <param name="ContractID"></param>
    /// <param name="CellFirstDay"></param>
    /// <param name="CellLastDay"></param>
    /// <param name="FirstDay"></param>
    /// <param name="LastDay"></param>
    /// <param name="PeriodType"></param>
    /// <returns></returns>
    [WebMethod]
    public RefreshSide_CalBackValue AddSurfaceItem(string SideID,
                                                string ContractID,
                                                string CellFirstDay,
                                                string CellLastDay,
                                                string FirstDay,
                                                string LastDay,
                                                int PeriodType)
    {
        DateTime dtFirstDay = DateTime.Parse(FirstDay);
        DateTime dtLastDay = DateTime.Parse(LastDay);

        DateTime dtCellFirstDay = DateTime.Parse(CellFirstDay);
        DateTime dtCellLastDay = DateTime.Parse(CellLastDay);



        wsContract ws = (wsContract)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Contract]; 
          
        Contract c = ws.GetContract(ContractID);
        ContractSurfaceItemList l = c.ContractSurfaceItemList;
        
        l.SetContractSurfaceItem(SideID, dtCellFirstDay, dtCellLastDay, ContractID, 0);        
        
        ws.SaveContract(c);

        string html = GetUpdatedHTML(SideID, c.ID, CellFirstDay, CellLastDay, FirstDay, LastDay, PeriodType, c.ContractSurfaceItemList);  
        return new RefreshSide_CalBackValue() { SideId = SideID, Html = html };
    }

    private string GetUpdatedHTML(string SideID,
                                                string ContractID,
                                                string CellFirstDay,
                                                string CellLastDay,
                                                string FirstDay,
                                                string LastDay,
                                                int PeriodType,
                                                ContractSurfaceItemList SurfaceItemList)
    {
        DateTime dtFirstDay = DateTime.Parse(FirstDay);
        DateTime dtLastDay = DateTime.Parse(LastDay); 

        SideShedule sh = SideShedule.GetSideShedule(SideID, dtFirstDay, dtLastDay);

        Ecopsy.Tools.DateTimeTools.TimePeriod Period = Ecopsy.Tools.DateTimeTools.TimePeriod.Month;
        switch (PeriodType)
        {
            case 1:
                Period = Ecopsy.Tools.DateTimeTools.TimePeriod.Day;
                break;
            case 2:
                Period = Ecopsy.Tools.DateTimeTools.TimePeriod.Week;
                break;
            case 3:
                Period = Ecopsy.Tools.DateTimeTools.TimePeriod.Month;
                break;
        }


        string html = SurfaceSheduleRowWriter.GetHtml(Period, sh, SurfaceItemList);
        return html;

    }
    
    
    [WebMethod]
    public RefreshSide_CalBackValue RefreshSide(string SideID,
                                                string ContractID,
                                                string CellFirstDay, 
                                                string CellLastDay,
                                                string FirstDay,
                                                string LastDay,
                                                int PeriodType)
    {

        DateTime dtFirstDay =DateTime .Parse (FirstDay) ;
        DateTime dtLastDay = DateTime.Parse(LastDay); 

        DateTime dtCellFirstDay=DateTime .Parse (CellFirstDay) ;
        DateTime dtCellLastDay = DateTime.Parse(CellLastDay);

        wsContract ws = (wsContract)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Contract]; 
        Contract c = ws.GetContract(ContractID);
        c.BusinesRulesEngine.OnError = OnError;
        ContractSurfaceItemList l = c.ContractSurfaceItemList;
        c.ContractSurfaceItemList.SetContractSurfaceItem(SideID, dtCellFirstDay, dtCellLastDay, c.ID, 0);        
        ws.SaveContract(c);
        
        CacheTools.ResetCache(wsAgency.wsKey_CurrentAgency);
 
        string html = GetUpdatedHTML(SideID, c.ID, CellFirstDay, CellLastDay, FirstDay, LastDay, PeriodType, c.ContractSurfaceItemList);  
     
        return new RefreshSide_CalBackValue() { SideId = SideID, Html = html };
        
    }

    private bool  OnError(List<string> l)
    {
        return true;
    }

    public class RefreshSide_CalBackValue 
    {
        public RefreshSide_CalBackValue(){}
        public string SideId { get; set; }
        public string Html { get; set; }


    }
    
    

}

